/**
 * 打开数据库
 * @param {object} dbName 数据库的名字
 * @param {string} version 数据库的版本
 * @return {object} 该函数会返回一个数据库实例
 */

function openDB(dbName: string, version = 1) {
  return new Promise((resolve, reject) => {
    const indexedDB = window.indexedDB;

    let db;

    // 打开数据库，若没有则会创建
    const request = indexedDB.open(dbName, version);

    // 数据库打开成功的回调
    request.onsuccess = function (event: any) {
      db = event.target.result; // 数据库对象
      console.log('🚀 ~ onsuccess ~: 数据库打开成功');
      resolve(db);
    };

    // 数据库打开失败的回调
    request.onerror = function (event: any) {
      console.log('🚀 ~ onsuccess ~: 数据库打开失败');
    };

    // 数据库有更新时候的回调
    request.onupgradeneeded = function (event: any) {
      // 数据库创建或升级的时候会触发
      console.log('🚀 ~ returnnewPromise ~ onupgradeneeded');
      db = event.target.result;
      // 创建存储库
      const objectStore = db.createObjectStore('signalChat', {
        keyPath: 'sequenceId', // 这是主键
        // autoIncrement: true // 实现自增
      });
      // 创建索引，在后面查询数据的时候可以根据索引查
      objectStore.createIndex('link', 'link', { unique: false });
      objectStore.createIndex('sequenceId', 'sequenceId', { unique: false });
      objectStore.createIndex('messageType', 'messageType', {
        unique: false,
      });
    };
  });
}

/**
 * 关闭数据库
 * @param {object} db 数据库实例
 */
function closeDB(db: any) {
  db.close();
  console.log('数据库已关闭');
}

/**
 * 新增数据
 * @param {object} db 数据库实例
 * @param {string} storeName 仓库名称
 * @param {string} data 数据
 */
function addData(db: any, storeName: any, data: any) {
  const request = db
    .transaction([storeName], 'readwrite') // 事务对象 指定表格名称和操作模式（"只读"或"读写"）
    .objectStore(storeName) // 仓库对象
    .add(data);

  request.onsuccess = function (event: any) {
    console.log('数据写入成功');
  };

  request.onerror = function (event: any) {
    console.log('数据写入失败');
  };
}
/**
 * 通过主键读取数据
 * @param {object} db 数据库实例
 * @param {string} storeName 仓库名称
 * @param {string} key 主键值
 */
function getDataByKey(db: any, storeName: any, key: any) {
  return new Promise((resolve, reject) => {
    const transaction = db.transaction([storeName]); // 事务
    const objectStore = transaction.objectStore(storeName); // 仓库对象
    const request = objectStore.get(key); // 通过主键获取数据

    request.onerror = function (event: any) {
      console.log('事务失败');
    };

    request.onsuccess = function (event: any) {
      console.log('主键查询结果: ', request.result);
      resolve(request.result);
    };
  });
}

/**
 * 更新数据
 * @param {object} db 数据库实例
 * @param {string} storeName 仓库名称
 * @param {object} data 数据
 */
function updateDB(db: any, storeName: any, data: any) {
  const request = db
    .transaction([storeName], 'readwrite') // 事务对象
    .objectStore(storeName) // 仓库对象
    .put(data);

  request.onsuccess = function () {
    console.log('数据更新成功');
  };

  request.onerror = function () {
    console.log('数据更新失败');
  };
}

/**
 * 通过主键删除数据
 * @param {object} db 数据库实例
 * @param {string} storeName 仓库名称
 * @param {object} id 主键值
 */
function deleteDB(db: any, storeName: any, id: any) {
  const request = db
    .transaction([storeName], 'readwrite')
    .objectStore(storeName)
    .delete(id);

  request.onsuccess = function () {
    console.log('数据删除成功');
  };

  request.onerror = function () {
    console.log('数据删除失败');
  };
}

/**
 * 删除数据库
 * @param {object} dbName 数据库名称
 */
function deleteDBAll(dbName: string) {
  console.log(dbName);
  const deleteRequest = window.indexedDB.deleteDatabase(dbName);
  deleteRequest.onerror = function (event) {
    console.log('删除失败');
  };
  deleteRequest.onsuccess = function (event) {
    console.log('删除成功');
  };
}

export {
  openDB,
  closeDB,
  deleteDBAll,
  addData,
  getDataByKey,
  updateDB,
  deleteDB,
};
